package com.whl.mybatis.helper;

import org.springframework.util.Assert;

import java.util.List;

public class PageHelper{
	private static ThreadLocal<Object> localPage = new ThreadLocal<Object>();
	public static boolean hasPagination() {
		if (localPage.get() != null) {
			return true;
		}
		return false;
	}

	public static <T>  void setPageInfo(PageInfo<T> pageInfo) {
		localPage.set(pageInfo);
	}

	@SuppressWarnings("unchecked")
	public static <T>  PageInfo<T> getPageInfo() {
		return (PageInfo<T>) localPage.get();
	}

	public static <T>  PageInfo<T> init(int pageNum, int pageSize) {
		PageInfo<T>  pageInfo = new PageInfo<T> ();
		pageInfo.setPageNum(pageNum);
		pageInfo.setPageSize(pageSize);
		setPageInfo(pageInfo);
		return pageInfo;
	}

	public static <T> PageInfo<T>  afterSetProperties(int total) {
		PageInfo<T>  pageInfo = getPageInfo();
		Assert.notNull(pageInfo, "分页参数有误");
		pageInfo.setTotal(total);
		return pageInfo;
	}

	public static <T>  PageInfo<T> afterSetProperties(List<T> list) {
		PageInfo<T> pageInfo = getPageInfo();
		Assert.notNull(pageInfo, "分页参数有误");
		pageInfo.setList(list);
		return pageInfo;
	}

	public static void destory() {
		if (hasPagination()) {
			localPage.remove();
		}
	}
}
